package com.docmgmt.mvp.mapper;

import com.docmgmt.mvp.entity.TagEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 标签表 Mapper 接口
 * </p>
 *
 * @author Dora
 */
@Mapper
public interface TagMapper extends BaseMapper<TagEntity> {

    /**
     * 根据标签名称查询标签
     *
     * @param name 标签名称
     * @return 标签实体
     */
    @Select("SELECT * FROM doc_tag WHERE name = #{name} LIMIT 1")
    TagEntity selectByName(@Param("name") String name);

    /**
     * 查询用户创建的所有标签
     *
     * @param userId 用户ID
     * @return 标签列表
     */
    @Select("SELECT * FROM doc_tag WHERE created_by = #{userId} ORDER BY created_at DESC")
    List<TagEntity> listByCreator(@Param("userId") Long userId);

    /**
     * 查询文件的所有标签
     *
     * @param fileId 文件ID
     * @return 标签列表
     */
    @Select("SELECT t.* FROM doc_tag t " +
            "INNER JOIN doc_file_tag ft ON t.id = ft.tag_id " +
            "WHERE ft.file_id = #{fileId} " +
            "ORDER BY ft.created_at DESC")
    List<TagEntity> listByFileId(@Param("fileId") Long fileId);
}
